var g = function (id){
    if( id.substr(0,1) == '.'){
        return document.getElementsByClassName(id.substr(1));
    }
    return document.getElementById(id);
};
var tip=[
            {des:"err1",content:"   用户名为空!"},
            {des:"err2",content:"   邮箱地址有误!"},
            {des:"err3",content:"   密码少于6位或超过20位!"},
            {des:"err4",content:"   密码不一致!"},
            {des:"err5",content:"   验证码不一致!"},
        ];
function isValidEmail(sText) {
        var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
        return reEmail.test(sText);
    }
$(document).ready(function () {
    $('[data-toggle="tooltip"]').tooltip();
    
    //google验证
	$("#getcode_gg").click(function(){
		$(this).attr("src",'code/code_gg.php?' + Math.random());
	});
    
    $('#reg').click(function(){
        $('#warn').empty();
        var code_gg = $("#code_gg").val();
        var name = $('#name').val();
        var email = $('#email').val();
        var password = $('#password').val();
        var conf_pass = $('#conf-pass').val();
        var pre_code = "<br/>&nbsp;&nbsp;&nbsp;&nbsp;"
        if(name==""){  //输出填写问题提示
            $('#warn').append(pre_code+tip[0].content);
            $('#al').removeAttr('hidden');
        }else if(!isValidEmail(email)){
            $('#warn').append(pre_code+tip[1].content);
            $('#al').removeAttr('hidden');
        }else if(password.length<6||password.length>20){
            $('#warn').append(pre_code+tip[2].content);
            $('#al').removeAttr('hidden');
        }else if(password!=conf_pass){
            $('#warn').append(pre_code+tip[3].content);
            $('#al').removeAttr('hidden');
        }else{
            $.post("code/chk_code.php?act=gg",{code:code_gg},function(msg){
                if(msg==0){
                    $('#warn').append(pre_code+tip[4].content);
                    $('#al').removeAttr('hidden');
                }else{               
                    $.post("reg_ck.php",{name:name,email:email,password:password},function(msg){                       
                        //alert(msg);
                        var data=eval('('+msg+')');
                        //alert(data);
                        if(data['code']==0){
                            alert('该用户名已被注册！');                          
                        }else if(data['code']==1){
                            alert('该邮箱已被注册！');
                        }else if(data['code']==2){
                            alert('注册成功，正跳转至登录页');
                            window.location.href='login.php';
                        }else{
                            alert('注册失败，请联系系统管理员zhonger#(@)wl27.cn');
                        };
                    });
                }
            });
        }  
        
    });
})